//
//  LPNewFeatureController.m
//  彩票
//
//  Created by liupan on 2016/12/5.
//  Copyright © 2016年 liupan. All rights reserved.
//

#import "LPNewFeatureController.h"
#import "LPNewFeatureViewCell.h"
@interface LPNewFeatureController ()
/** 记录上一次的偏移量 */
@property (nonatomic,assign)CGFloat lastOffsetX;
/** 球 */
@property (nonatomic,weak)UIImageView *guide;
/** 大标题 */
@property (nonatomic,weak)UIImageView *guideLargeText;
/** 小标题 */
@property (nonatomic,weak)UIImageView *guideSmallText;
@end

@implementation LPNewFeatureController

static NSString * const reuseIdentifier = @"Cell";
//重写init方法
- (instancetype)init{
//流水布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //item的大小等于屏幕的大小
    layout.itemSize = [UIScreen mainScreen].bounds.size;
    //行与行之间的最小行间距
    layout.minimumLineSpacing = 0;
    //同一行的cell中互相之间的最小间隔
    layout.minimumInteritemSpacing = 0;
    //水平方向滑动
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    return [super initWithCollectionViewLayout:layout];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.collectionView registerClass:[LPNewFeatureViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    //启动分页
    self.collectionView.pagingEnabled=YES;
    //禁止弹簧效果
    self.collectionView.bounces = NO;
    //隐藏水平滚动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
    //添加所有子控件
    [self setupAddChildImageView];
}
/**
 添加所有子控件
 */
- (void)setupAddChildImageView{
    //线
    UIImageView *guideLine = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guideLine"]];
    //添加到collectionView
    [self.collectionView addSubview:guideLine];
    guideLine.x -= 170;
    //球
    UIImageView *guide = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guide1"]];
    guide.centerX = self.view.centerX;
    //添加到collectionView
    [self.collectionView addSubview:guide];
    self.guide=guide;
    //大标题
    UIImageView *guideLargeText = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guideLargeText1"]];
    //添加到collectionView
    [self.collectionView addSubview:guideLargeText];
    self.guideLargeText = guideLargeText;
    guideLargeText.center = CGPointMake(self.view.width * 0.5, self.view.height * 0.7);
    //小标题
    UIImageView *guideSmallText = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guideSmallText1"]];
    //添加到collectionView
    [self.collectionView addSubview:guideSmallText];
    self.guideSmallText = guideSmallText;
    guideSmallText.center = CGPointMake(self.view.width * 0.5, self.view.height * 0.8);
}
#pragma mark- <UICollectionViewDataSource>
#define LPCount 4
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return LPCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LPNewFeatureViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    NSString *name = [NSString stringWithFormat:@"guide%ldBackground",indexPath.item + 1];
    cell.image = [UIImage imageNamed:name];
    //告诉Cell什么时候是最后一行
    [cell setupIndex:indexPath count:LPCount];
    
    return cell;
}
- (BOOL)prefersStatusBarHidden{
    return YES;
}
#pragma mark -滑动屏幕加载
// 滚动视图减速完成，滚动将停止时，调用该方法。一次有效滑动，只执行一次。
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //总偏移量
    CGFloat offsetX = scrollView.contentOffset.x;
    //计算一个偏移量
    CGFloat del = offsetX - self.lastOffsetX;
    //上一次偏移量
    self.lastOffsetX = offsetX;
    //切换图片
    //球
        NSInteger page = offsetX / self.view.width;
        NSString *guiname = [NSString stringWithFormat:@"guide%ld",page + 1];
        self.guide.image = [UIImage imageNamed:guiname];
    //大标题
    NSString *largename = [NSString stringWithFormat:@"guideLargeText%ld",page + 1];
    self.guideLargeText.image = [UIImage imageNamed:largename];
    //小标题
    NSString *smallname = [NSString stringWithFormat:@"guideSmallText%ld",page + 1];
    self.guideSmallText.image = [UIImage imageNamed:smallname];
    self.guide.x += del *2;
    self.guideLargeText.x += del *2;
    self.guideSmallText.x += del *2;
    [UIView animateWithDuration:0.25 animations:^{
        self.guide.x -= del;
        self.guideLargeText.x -= del;
        self.guideSmallText.x -= del;
    }];
}
// 滑动减速时调用该方法。
//- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
//
//
//}
@end




















